﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ModalDownload.Models;
using System.Data;

namespace ModalDownload.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Nav = "Index";
            List<ClassTable> list = ClassTable.GetAllParentClass("3d");
            GetWebSettings(ViewData);
            return View(list);
        }

        public ActionResult List(string type,string cat,string page)
        {
            if (string.IsNullOrEmpty(type))
                type = "3d";
            ViewBag.Nav = type;
            int p = 0;
            int.TryParse(page, out p);
            if (p < 1)
                p = 1;
            int classid = 0;
            int.TryParse(cat,out classid);
            if (classid<=0)
            {
                ViewBag.Title = ClassTable.GetAllTypes().FirstOrDefault(t => t.Value == type).Text;
                ViewBag.Parent = ViewBag.Title;
            }
            else
            {
                ViewBag.Title = ClassTable.GetClassByID(cat).classname;
                ViewBag.Parent = ClassTable.GetAllTypes().FirstOrDefault(t => t.Value == ClassTable.GetClassByID(cat).type).Text;
            }
            ViewBag.Cat = cat;
            ViewBag.Type = type;
            ViewBag.Total = ModelTable.GetTotal(type,classid);
            ViewBag.Page = p;
            ViewBag.PageSize = 27;
            ViewBag.PageLength = Math.Ceiling((decimal)ViewBag.Total / ViewBag.PageSize);
            GetWebSettings(ViewData);
            return View();
        }

        public ActionResult Detail(string id)
        {
            if(string.IsNullOrEmpty(id))
                return RedirectToAction("Index", "Home");
            ViewBag.ID = id;
            GetWebSettings(ViewData);
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "你的应用程序说明页。";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "你的联系方式页。";

            return View();
        }

        public static void GetWebSettings(ViewDataDictionary viewdata)
        {
            DataTable dt=SQL_MS.myTable("select * from settings",CommandType.Text,null);
            foreach(DataRow dr in dt.Rows)
            {
                if (dr["key"].ToString() == "sitename")
                    viewdata["sitename"] = dr["value"].ToString();
                else if (dr["key"].ToString() == "siteurl")
                    viewdata["siteurl"] = dr["value"].ToString();
                else if (dr["key"].ToString() == "icp")
                    viewdata["icp"] = dr["value"].ToString();
                else if (dr["key"].ToString() == "notice")
                    viewdata["notice"] = dr["value"].ToString();
            }
        }

        public ActionResult Search()
        {
            GetWebSettings(ViewData);
            return View();
        }

        public ActionResult SearchResult(string q)
        {
            List<ModelTable> list=ModelTable.GetModelsLikeTitle(q);
            GetWebSettings(ViewData);
            return View(list);
        }

        public ActionResult Mail()
        {
            ViewBag.Nav = "mail";
            GetWebSettings(ViewData);
            return View();
        }

        public ActionResult Buy()
        {
            ViewBag.Nav = "buy";
            GetWebSettings(ViewData);
            return View();
        }

    }
}
